'''
for 列表表达式
'''
a_range = range(10)
# 对a_range执行for表达式
a_list = [x * x for x in a_range]
# a_list集合包含10个元素
print(a_list)

''' if限定 '''
b_list = [x * x for x in a_range if x % 2 == 0]
print(type(b_list))
# a_list集合包含5个元素
print(b_list)

'''生成器(元组)'''
c_generator = (x * x for x in a_range if x % 2 == 0)
print(type(c_generator))
# 使用for循环迭代生成器
for i in c_generator:
    print(i, end='\t')
print()

'''循环嵌套'''

d_list = [(x, y) for x in range(5) for y in range(4)]
# d_list列表包含20个元素
print(d_list)

# 等价于以下代码

dd_list = []
for x in range(5):
    for y in range(4):
        dd_list.append((x, y))

src_a = [30, 12, 66, 34, 39, 78, 36, 57, 121]
src_b = [3, 5, 7, 11]
# 只要y能整除x，就将它们配对在一起
result = [(x, y) for x in src_b for y in src_a if y % x == 0]
print(result)

'''矩阵倒置 行转列
1 2  
3 4 -->1 3 5 
5 6    2 4 6  
'''
m = [[1, 2], [3, 4], [5, 6]]
for row in m:
    print(row)
# m[0][0] m[1][0] m[2][0]
# m[0][1] m[1][1] m[2][1]
# 如果是双重[]列表表达式，是从后往前迭代
rez = [[m[j][i] for j in range(3)] for i in range(2)]  # len(m)  len(m[0])
print("\n")
for row in rez:
    print(row)
